<template>
  <div class="modal" :class="{ 'modal-open': modalConfig.visible }">
    <div class="modal-content">
      <div class="modal-header">
        {{ modalConfig?.title || '弹窗标题' }}
        <span class="modal-close" @click="closeModal(false)">&times;</span>
      </div>
      <div class="modal-body">
        <slot name="body"></slot>
      </div>
      <div class="modal-footer">
        <button @click="closeModal(true)" style="margin-right: 20px">确定</button>
        <button @click="closeModal(false)">取消</button>
      </div>
    </div>
  </div>
</template>

<script setup>
const _props = defineProps({
  modalConfig: {
    type: Object,
    required: true,
    default: () => ({
      title: '弹窗标题',
      width: 300,
      zIndex: 1,
      visible: false,
    }),
  },
})
const _emits = defineEmits(['update:modalConfig', 'modalClose'])
function closeModal(is) {
  _emits('update:modalConfig', { ..._props.modalConfig, visible: false })
  _emits('modalClose', is)
}
</script>

<style scoped lang="less">
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9999;
}

.modal-open {
  display: block;
}

.modal-content {
  background-color: #fff;
  max-width: 40%;
  min-width: 460px;
  margin: 100px auto;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.modal-header {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-close {
  cursor: pointer;
}

.modal-body {
  margin-bottom: 20px;
}

.modal-footer {
  text-align: right;
}
</style>
